#Matan Keidar 066500992
#Eliahu Khalastchi 040854705

from Corpus import Corpus

def zeroList(n):
    '''
    creates a new list initialized to n zeros
    '''
    retList = []
    
    for i in range(n):
        retList.append(0.0)
    
    return retList

def zeroMatrix(n,m):
    '''
    creates a new list of lists initialized to mxn zeros 
    '''
    retMat = []
    
    for i in range(n):
        retMat.append(zeroList(m))
 
    return retMat

def parseSetFile(fileName, topicsList):
    '''
    parse set input file
    returns: corpus for each document, total number of documents read, list of unique words and its length , topics of each read document
    '''
    
    corpusList = []
    numDocs = 0
    wordSet= set()
    
    # open file
    f = open(fileName, "r")
    
    # read first line
    line = f.readline()
    docTopicList = []
    
    # scan the entire file
    while  not line == "":
        # <TRAIN    5    grain    wheat    corn>
        listTopics = []
        i = 0
        
        # scan each word in TRAIN file and get the topics
        for w in line.strip().split():
            word=w
            if i>1:
                if word[-1] == ">":
                    word=word[:-1]
                
                for tup in topicsList:
                    if tup[0] == word:
                        listTopics.append(tup[1])
                 
            i += 1
            
        docTopicList.append(listTopics)
        
        # read another line and parse the article
        line=f.readline()
        numDocs += 1
        
        corpusDocY = Corpus()
        
        # add each word from the article to a new corpus
        for word in line.split():
            corpusDocY.addWord(word)
            wordSet.add(word)
        
        corpusList.append(corpusDocY)
        
        
        line = f.readline()
        
    f.close()
    
    return corpusList, numDocs, list(wordSet), len(wordSet) , docTopicList
    
def parseTopicsFile(fileName):
    '''
    parse the topics file
    '''
    topicsList = []
    counter = 0
    
    # open topics file
    f = open(fileName, "r")
   
    # scan each topic in file
    for line in f.readlines():
        line = line.strip()
        
        topicsList.append((line, counter))
        counter += 1
        
    f.close()
    
    return counter, topicsList    
        
def parseInputs(setFileName, topicsFileName):
    '''
    parse input files
    returns
    '''
    numTopics, topicsDic = parseTopicsFile(topicsFileName)
    
    return (numTopics,topicsDic), parseSetFile(setFileName, topicsDic)


#def putInDic(dic,len,i,j,val):
#    '''
#    '''    
#    dic[i*len+j]=val
#    
#def getFromDic(dic,len,i,j):    
#    return dic[i*len+j]